package hot100;

public class Lc543 {
    public class TreeNode {
       int val;
       TreeNode left;
       TreeNode right;
       TreeNode() {}
       TreeNode(int val) { this.val = val; }
       TreeNode(int val, TreeNode left, TreeNode right) {
           this.val = val;
           this.left = left;
           this.right = right;
       }
    }

    private int ret = 0;
    public int diameterOfBinaryTree(TreeNode root) {
        dfs(root);
        return this.ret;
    }

    private int dfs(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            return 1;
        }
        int right = dfs(root.right);
        int left = dfs(root.left);
        this.ret = Math.max(right + left, ret);
        return Math.max(right, left) + 1;
    }
}
